$(function () {
    var ajaxupload =new AjaxUpload($('#test8'),{
        action:  baseURL + "engine/param/importExcel",
        type:'POST',
        data:{},
        autoSubmit:false,
        responseType:'json',
        name:'file',
        onChange: function(file, ext){
            if(ext && !(/^(xlsx)$/.test(ext) )){
                alert("只支持xlsx格式的文件");
            }else{
                $("#fileName").val(file);
            }
        },
        onComplete: function(file, resp){
            if(resp.code === 0){
                layer.msg("导入成功", {icon: 1});
                $("#fileName").val('');
            }else if(resp.code === 500){
                $("#fileName").val('');
                layer.msg(resp.msg, {icon: 2});
            }
        }
    });
    $("#test9").click(function(){
        ajaxupload.setData({id:vm.engineObj.id});
        ajaxupload.submit();
    });

});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        engineItem: {
            paramIdList: [],
            name: null
        },
        paramList: [],
        id : '',
        engineObj:{},
        showTree: true,
        style: {width: '20%',float: 'left'},
        showImport: false,

    },
    created () {
        this.initTree()
    },
    methods: {
        initTree: function (){
            // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
            var setting = {
                callback: {
                    onClick: this.chooseNode
                }
            }
            $.get(baseURL + "engine/manage/listnopage/", function(r){
                var children = r.page
                var zNodes = [
                    {
                        name: "发动机",
                        open: true,
                        children: children
                    }
                ];
                $.fn.zTree.init($("#tree"), setting, zNodes);
                new MtrSearchZTree();
            });
        },
        chooseNode :function (e,tId,tNode){
            if(tNode.tId != 'tree_1'){
                vm.engineObj = tNode
                $.jgrid.gridUnload('#jqGrid')
                this.initGrid()
            }
        },
        initGrid: function  (){
            $.get(baseURL + "engine/param/listColumn",{tableName:vm.engineObj.tableName}, function(r){
                $("#jqGrid").jqGrid({
                    url: baseURL + 'engine/param/list',
                    //datatype: "local",
                    datatype: "json",
                    colModel: r.ret,
                    viewrecords: true,
                    height: 385,
                    rowNum: 10,
                    rowList : [10,30,50],
                    rownumbers: true,
                    rownumWidth: 25,
                    autowidth:true,
                    multiselect: true,
                    pager: "#jqGridPager",
                    jsonReader : {
                        root: "page.list",
                        page: "page.currPage",
                        total: "page.totalPage",
                        records: "page.totalCount"
                    },
                    prmNames : {
                        page:"page",
                        rows:"limit",
                        order: "order"
                    },
                    postData : {tableName:vm.engineObj.tableName},
                    gridComplete:function(){
                        //隐藏grid底部滚动条
                        $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                    }
                });
                /*for ( var i = 0; i <= r.ret.ret.length; i++){
                    jQuery("#jqGrid").jqGrid('addRowData', i + 1, r.ret.ret[i]);
                }*/
            });
        },

        query: function () {
            vm.reload();
        },
        add: function(){
            vm.style = {width: '20%',float: 'left',display:'none'}
            vm.showList = false;
            vm.title = "新增";
            vm.paramList = {};
            this.getParam()

        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.style = {width: '20%',float: 'left',display:'none'}
            vm.showList = false;
            vm.title = "修改";
            vm.paramList = {};
            //this.getParam()

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var paramRet = {};
                paramRet.list = vm.paramList;
                paramRet.tableName = vm.engineObj.tableName;
                paramRet.id = vm.id;

                var url = vm.title == "新增" ? "engine/param/save" : "engine/param/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(paramRet),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var idss = getSelectedRows();
            var ids = []
            for(var id in idss){
                ids.push(getSelectedData(idss[id]).id)
            }
            var mapParam = {}
            mapParam.ids = ids
            mapParam.tableName = vm.engineObj.tableName
            if(ids == null){
                return ;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "engine/param/delete",
                        contentType: "application/json",
                        data: JSON.stringify(mapParam),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                vm.reload();
                                //$("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },
        getInfo: function(id){
            $.get(baseURL + "engine/param/getParam/",vm.engineObj, function(r){
                var obj = $("#jqGrid").jqGrid('getRowData', id);
                for (var i=0 ; i<r.paramList.length ; i++){
                    r.paramList[i].name = 'param_' + r.paramList[i].id
                    for (var key in obj) {
                        if(r.paramList[i].name == key){
                            r.paramList[i].ret = obj[key]
                        }
                    }
                }
                vm.paramList = r.paramList;
                vm.id = obj.id
            });

        },
        getParam: function(){
            $.get(baseURL + "engine/param/getParam/",vm.engineObj, function(r){
                for (var i=0 ; i<r.paramList.length ; i++){
                    r.paramList[i].name = 'param_' + r.paramList[i].id
                }
                vm.paramList = r.paramList;
            });
        },
        reload: function (event) {
            /*vm.showList = true;
            vm.style = {width: '20%',float: 'left'}
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid("clearGridData")
            vm.initGrid()*/
            vm.style = {width: '20%',float: 'left'}
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");

        },
        importReload: function (event) {
            /*vm.showList = true;
            vm.style = {width: '20%',float: 'left'}
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid("clearGridData")
            vm.initGrid()*/
            vm.style = {width: '20%',float: 'left'}
            vm.showList = true;
            vm.showImport = false;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");

        },
        import: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        },
        download: function (event) {
            if(vm.engineObj.id === undefined){
                alert("请选择一台发动机");
                return ;
            }
            var url = "engine/param/download/"+vm.engineObj.id;
            window.location.href = baseURL + url;
        },
        exportExcel: function (event) {
            if(vm.engineObj.id === undefined){
                alert("请选择一台发动机");
                return ;
            }
            var url = "engine/param/export/"+vm.engineObj.id;
            window.location.href = baseURL + url;
        },
        openImport:function (event) {
            if(vm.engineObj.id === undefined){
                alert("请选择一台发动机");
                return ;
            }
            vm.style = {width: '20%',float: 'left',display:'none'}
            vm.showList = false;
            vm.showImport = true;
            vm.title = "导入";
            vm.paramList = {};
        },
    }
});